package com.ll.gulimall.product.service.impl;

import com.ll.gulimall.product.entity.AttrEntity;
import com.ll.gulimall.product.service.AttrService;
import com.ll.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.ll.gulimall.product.vo.SpuItemBaseAttrVo;
import com.ll.gulimall.product.vo.SpuSaleAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ll.common.utils.PageUtils;
import com.ll.common.utils.Query;

import com.ll.gulimall.product.dao.AttrGroupDao;
import com.ll.gulimall.product.entity.AttrGroupEntity;
import com.ll.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            //如果检索非等于空则追加条件
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0L) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
        wrapper.eq("catelog_id", catelogId);
    }
    IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }


    @Autowired
    private AttrService attrService;
    /**
     * 根分类Id查询该分组的所有属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId)
    {
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> vos = groupEntities.stream().map(group -> {
            // 封装属性分组
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, vo);
            // 封装属性
            List<AttrEntity> attr = attrService.getRelation(vo.getAttrGroupId());
            vo.setAttrs(attr);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    /**
     * 根据spuid 和 catalogid 规格与包装
     * @param spuId
     * @param catalogId
     * @return
     */
    @Override
    public List<SpuItemBaseAttrVo> getAttrGroupWithAttrsBySpuIdCatalogId(Long spuId, Long catalogId) {

        return this.baseMapper.getAttrGroupWithAttrsBySpuIdCatalogId(spuId,catalogId);
    }


}